package com.controller.xsxt;

import com.entity.common.ApiResult;
import com.service.xsxt.DormService;
import com.entity.xsxt.Dorm;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by System on 2019/04/15 09:13:54.
 */
@RestController
@RequestMapping("dorm")
public class DormController {

    @Autowired
    private DormService dormService;

    @RequestMapping("/getPageList")
    public Map<String,Object> getPageList(@RequestParam(value = "pager.pageNo",defaultValue ="1" )  Integer pageNum,
                @RequestParam(value = "pager.pageSize",defaultValue ="10" ) Integer pageSize,Dorm dorm){
        Map<String, Object> map=new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Dorm> pageInfo = new PageInfo<>(dormService.getPageList(dorm));
        map.put("rows",pageInfo.getList());
        map.put("pager.totalRows", pageInfo.getTotal());
        map.put("pager.pageNo",pageNum);
        return  map;
    }


    @RequestMapping("/getAllData")
    public Map<String,Object> getAllData(Dorm dorm){
        Map<String, Object> map=new HashMap<>();
        map.put("list",dormService.getPageList(dorm));
        return  map;
    }

    @RequestMapping("/addDorm")
    public ApiResult addDorm(Dorm dorm){
        dorm.setDormCode(CommonUtil.getUUID());
        return dormService.addDorm(dorm);
    }

    @RequestMapping("/deleteDorm")
    public ApiResult deleteDorm(Long id){
        return dormService.deleteDorm(id);
    }

    @RequestMapping("/getDormById")
    public ApiResult getDormById(Long id){
        return dormService.getDormById(id);
    }

    @RequestMapping("/editDorm")
    public ApiResult editDorm(Dorm dorm){
        return dormService.editDorm(dorm);
    }

}
